using UnityEngine;
using System;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// <para>事件教学节点，用于GUI点击事件提示，点击后触发事件</para>
/// </summary>
public class GuiClickTutorialNode : TutorialEventNode
{
    public enum ClickType
    {
        UI,
        Building,
    }

    public enum FingerType
    {
        Left,
        Right
    }

    [JsonAttribute("cktp", "点击类型")]
    public ClickType clickType = ClickType.UI;

    [JsonAttribute("fgtp", "手指类型")]
    public FingerType fingerType = FingerType.Left;

    [JsonAttribute("uiPath", "UI路径", "cktp", "UIprefab对象, 子节点路径", "UI")]
	public List<string> uiPath = new List<string>() { "",""};

	[JsonAttribute("bdid", "建筑ID", "cktp","", "Building")]
    public int buildingID = 0;

	[JsonAttribute("dstEnd", "结束后是否删除UI(暂时废弃)")]
    public bool _DestoryOnEnd = true;

    [JsonAttribute("mask", "显示遮罩")]
    public bool _enableMasks = true;

    [JsonAttribute("maAl", "遮罩透明度", "mask")]
    public float _maskAlpha = 0.4f;

	[JsonAttribute("frm", "显示外框")]
	public bool _enableFrame = true;
	[JsonAttribute("ptr", "显示指针")]
	public bool _enablePointer = true;
	[JsonAttribute("agl", "指针角度", "ptr")]
	public float _pointerAngle = 180f;
	[JsonAttribute("move", "指针移动", "ptr")]
	public bool _isPointerMove = false;
	[JsonAttribute("range", "移动幅度(单位:像素)", "move")]
	public float _moveRange = 0;
	[JsonAttribute("scl", "缩放")]
	public float _scale = 1f;

	TutorialClick _tutorialClick = null;
    Transform root = null;
	RectTransform _targetTrans = null;
	Vector3 _originalScale;
    CustomBuildGoBehaviour building;


    public string parentPath;
    public string parentName;
    public string childPath;

    TutorialPointer pointer;

#if UNITY_EDITOR
    [JsonEditorAttribute(typeof(UIPathSwitchMember), "uiPath")]
    public List<object> selectUI = new List<object>();
#endif

    protected override void Init()
	{
        switch (clickType)
        {
            case ClickType.UI:
                UITools.GetResourcePath(uiPath[0], out parentPath, out parentName);
                childPath = UITools.GetTutorialChildPath(uiPath[1]);
                var panelObj = Hope.UIManager.Instance.GetPanel(parentName);
				if (!panelObj || panelObj.transform.localScale == Vector3.zero)
                {
                    Hope.UIManager.Instance.ShowPanel(parentName, parentPath, (GameObject obj) => {
                        root = obj.transform;
                        _tutorialManager.StartCoroutine(OnOpenUI());
                    }, (GameObject obj) =>
                    {
                        root = obj.transform;
                        _tutorialManager.StartCoroutine(OnOpenUI());
                    });
                }
                else
                {
                    root = panelObj.transform;
                    _tutorialManager.StartCoroutine(OnOpenUI());
                }
                _tutorialManager.lateUpdate += Update;
                break;
            case ClickType.Building:
                CustomBuildController.Instance.SetNotCanClickBuilding();
                building = TutorialLogicUtils.GetBuildIngWithBuildingID(buildingID);
                _targetTrans = TutorialUIManager.Instance.GetTempForBuildingMask(
                    building.boxCollider,
                    Hope.UIManager.Instance.UIRoot.GetComponent<Canvas>());
                ClickTarget(_targetTrans);
                break;
            default:
                break;
        }
	}

    public void Update()
    {
        if(TutorialUIManager.Instance.new_focusPointer != null && _targetTrans != null)
        {
            if(TutorialUIManager.Instance.new_focusPointer.transform.position != _targetTrans.position)
            {
                TutorialUIManager.Instance.new_focusPointer.transform.position = _targetTrans.position;
                if (_enableMasks)
                {
                    TutorialUIManager.Instance.ShowFocusMasks(_targetTrans, Hope.UIManager.Instance.UIRoot.GetComponent<Canvas>(), _maskAlpha);
                }
            }
            
        }
    }

    public void ClickTarget(RectTransform target)
    {
        AddTutorialComponent(new UIOperitionComponent());
        // 在这里自己调用的原因，是因为已经错过了调用期
        OnCompontListEnter();

        var uiRoot = Hope.UIManager.Instance.UIRoot;
        var rootCanvas = uiRoot.GetComponent<Canvas>();
        if (target)
        {
            _tutorialClick = target.gameObject.AddComponent<TutorialClick>();
            _tutorialClick.Node = this;
            if (_enableMasks)
            {
                TutorialUIManager.Instance.ShowFocusMasks(target, rootCanvas, _maskAlpha);
                InputUtils.TutorialDisable();
            }
            if (_enableFrame)
                TutorialUIManager.Instance.ShowFocusFrame(target, rootCanvas);
            if (_enablePointer)
                TutorialUIManager.Instance.New_ShowFocusPointer(target, rootCanvas, _pointerAngle, _isPointerMove, _moveRange, fingerType);
        }
    }

    
    IEnumerator OnOpenUI()
    {
        yield return new WaitForEndOfFrame();
        yield return new WaitForEndOfFrame();
        _targetTrans = root.Find(childPath).GetComponent<RectTransform>();
        _originalScale = _targetTrans.localScale;
        _targetTrans.localScale *= _scale;
        if (_targetTrans)
        {
            ClickTarget(_targetTrans);
        }
        else
            Debug.LogWarning("Warning! No button found path: " + childPath);
        yield return null;
    }

    protected override void OnShoot()
    {
        switch (clickType)
        {
            case ClickType.UI:
                if (_DestoryOnEnd)
                {
//                    Hope.UIManager.Instance.ClosePanel(parentName);
                }
                break;
            case ClickType.Building:
                building.InputHandle.OnClick();
                GameObject.Destroy(_targetTrans.gameObject);
                break;
            default:
                break;
        }
    }

    protected override void Cancel()
    {
        
        if (_targetTrans == null)
            return;
		_targetTrans.localScale = _originalScale;
		if (_tutorialClick)
			UnityEngine.Object.Destroy(_tutorialClick);
		if (_enableMasks) {
			TutorialUIManager.Instance.HideFocusMasks();
			InputUtils.TutorialEnable();
		}
		if (_enableFrame)
			TutorialUIManager.Instance.HideFocusFrame();
		if (_enablePointer)
			TutorialUIManager.Instance.New_HideFocusPointer();
        switch (clickType)
        {
            case ClickType.UI:
                _tutorialManager.lateUpdate -= Update;
                break;
            case ClickType.Building:
                CustomBuildController.Instance.SetCanClickBuilding();
                break;
            default:
                break;
        }
    }
}
